package com.qcxy.gzy.activity;

import java.util.Timer;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.qcxy.gzy.activity.v2.TotalActivity;
import com.qcxy.gzy.activity.v2.TrainingActivity;
import com.qxcy.gzy.util.SPUtils;

/**
 * 时间界面 <br>
 * 
 * @author gzy
 * 
 */
public class TimeActivity extends TotalActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mTargetNumber.setText(R.string.target_time);
		mUnit.setText(R.string.time_uint);

		mSeekBar.setMax(60);
		
		mSeekBar.setProgress(Integer.parseInt(SPUtils.getValueFromSharedFile(this, "TargetTime", 0+"")));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_go: {
			SPUtils.setValueSharedFile(this, "TargetTime", mTarget+"");
			Intent intent = new Intent(TimeActivity.this,
					TrainingActivity.class);
			intent.putExtra("Target", mTarget + "");
			intent.putExtra("Activity", "Time");
			startActivity(intent);
			TimeActivity.this.finish();
			break;
		}

		case R.id.iv_back: {
			TimeActivity.this.finish();
			break;
		}
		
		case R.id.iv_target_reduce: {
			if (mTarget > 0) {
				mTarget--;
				mTargetValue.setText(String.valueOf(mTarget));
				mSeekBar.setProgress(mTarget);
			}
			break;
		}

		case R.id.iv_target_plus: {
			if (mTarget < 60) {
				mTarget++;
				mTargetValue.setText(String.valueOf(mTarget));
				mSeekBar.setProgress(mTarget);
			}
			break;
		}

		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			timer = new Timer(true);
			switch (v.getId()) {
			case R.id.iv_target_reduce:
				timer.schedule(new TimeTask(1), 1000, 100);
				break;

			case R.id.iv_target_plus:
				timer.schedule(new TimeTask(3), 1000, 100);
				break;
			}
			break;

		case MotionEvent.ACTION_UP:
			timer.cancel();
			timer.purge();
			timer = null;
			break;

		case MotionEvent.ACTION_CANCEL:
			timer.cancel();
			timer.purge();
			timer = null;
			break;
		}
		return false;
	}
}
